package com.zyf.springboot.controller;

import com.zyf.springboot.entity.Book;
import com.zyf.springboot.entity.Bookinfor;
import com.zyf.springboot.entity.Result;
import com.zyf.springboot.mapper.BookMapper;
import com.zyf.springboot.mapper.BookinforMapper;
import org.apache.ibatis.annotations.Param;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.xml.crypto.Data;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

@RestController
public class BookController {
    @Resource
    BookMapper bookMapper;
    @Resource
    BookinforMapper bookinforMapper;

    @PostMapping("/getBookRiZhi")
    public Result getBookRiZhi() {
      return new Result<>(0,"success",1000,bookMapper.selectAll());

    }

    @PostMapping("/demo1")
    public List<Bookinfor> demo1() {
        return bookinforMapper.selectBookInfor();
    }

    @PostMapping("/outBook")
    public String  outBook(String bookName) {
        Bookinfor bookinfor = bookinforMapper.selectBookInforByName(bookName);
        //图书已被借出
        if (bookinfor.getState() == 0) {
            return "图书已被借出";
        }

        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("YYYY-MM-dd HH:mm:ss");
        Date date = new Date();
        String format = simpleDateFormat.format(date);
        bookMapper.outBook(bookName, format,"未归还");
        bookinforMapper.updateBook(bookName, 0);
        return "借书成功";
    }

    @PostMapping("/inBook")
    public String  inBook(String bookName) {
        Bookinfor bookinfor = bookinforMapper.selectBookInforByName(bookName);
        //图书已被归还
        if (bookinfor.getState() == 1) {
            return "图书已被归还,请检查归还书名";
        }
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("YYYY-MM-dd HH:mm:ss");
        Date date = new Date();
        String format = simpleDateFormat.format(date);
        bookMapper.inBook(bookName, format);
        bookinforMapper.updateBook(bookName, 1);
        return "归还成功";
    }

    @PostMapping("/demo3")
    public void demo3(@Param("bookName") String bookName, @Param("state") int state) {
        bookinforMapper.updateBook(bookName, state);
    }
}
